<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>模型介绍 | CatchAdmin 官网</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.2">
    <style>a[title="站长统计"]{display:none}</style>
    <script data-ad-client="ca-pub-1505209242532150" async="true" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <script src="https://v1.cnzz.com/z_stat.php?id=1279921342&amp;web_id=1279921342"></script>
    <meta name="description" content="基于 Thinkphp & Vue 开发的后台管理框架">
    <meta name="keywords" content="catchadmin, 后台管理, 前后端分离, thinkphp后台管理框架, thinkphp前后端分离框架,php, elementui">
    <link rel="preload" href="/assets/css/0.styles.6834c255.css" as="style"><link rel="preload" href="/assets/js/app.d0396f44.js" as="script"><link rel="preload" href="/assets/js/2.1cef47df.js" as="script"><link rel="preload" href="/assets/js/17.aac0bf62.js" as="script"><link rel="prefetch" href="/assets/js/10.483f2013.js"><link rel="prefetch" href="/assets/js/11.981d4461.js"><link rel="prefetch" href="/assets/js/12.95d2c383.js"><link rel="prefetch" href="/assets/js/13.061c2fe0.js"><link rel="prefetch" href="/assets/js/14.ec2753db.js"><link rel="prefetch" href="/assets/js/15.8ba1c321.js"><link rel="prefetch" href="/assets/js/16.f7f0d34c.js"><link rel="prefetch" href="/assets/js/18.2e2fe306.js"><link rel="prefetch" href="/assets/js/19.aee304b3.js"><link rel="prefetch" href="/assets/js/20.86a73bc1.js"><link rel="prefetch" href="/assets/js/21.8992c972.js"><link rel="prefetch" href="/assets/js/22.0a78500a.js"><link rel="prefetch" href="/assets/js/23.06342909.js"><link rel="prefetch" href="/assets/js/24.06fd96a3.js"><link rel="prefetch" href="/assets/js/25.00664d4e.js"><link rel="prefetch" href="/assets/js/26.c3d4b548.js"><link rel="prefetch" href="/assets/js/27.3c6927ad.js"><link rel="prefetch" href="/assets/js/28.74498c0b.js"><link rel="prefetch" href="/assets/js/29.aae1a826.js"><link rel="prefetch" href="/assets/js/3.4d2bdca7.js"><link rel="prefetch" href="/assets/js/30.c7732a2c.js"><link rel="prefetch" href="/assets/js/31.20403043.js"><link rel="prefetch" href="/assets/js/32.7b8e9cfe.js"><link rel="prefetch" href="/assets/js/33.8610da11.js"><link rel="prefetch" href="/assets/js/4.d7b1015b.js"><link rel="prefetch" href="/assets/js/5.ff87de2e.js"><link rel="prefetch" href="/assets/js/6.0e91e688.js"><link rel="prefetch" href="/assets/js/7.ba30e213.js"><link rel="prefetch" href="/assets/js/8.35122549.js"><link rel="prefetch" href="/assets/js/9.081b18e5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.6834c255.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active no-logo home-link"><!----> <span class="site-name">CatchAdmin 官网</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          🏠首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item ant-menu-item-selected"><a href="/docs/" class="router-link-active">
          📖文档
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/laravel/">
          📖Laravel
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/faq/">
          🤔FAQ
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/donate/">
          🎉赞助名单
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🚀仓库地址
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <!----></nav></div></div> <!----></header> <aside class="sidebar"><div><div class="ads"><div id="ads_1"><img src="https://cover.kancloud.cn/akasishikelu/thinkphp6!middle"> <span title="基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析">基于 catchadmin 的 Thinkphp 6.0 企业级后台管理开发&amp;源码分析</span></div></div> <div role="separator" id="reset-margin" class="ant-divider ant-divider-horizontal ant-divider-dashed"></div></div> <ul class="sidebar-links"><li><a href="/docs/" aria-current="page" title="项目介绍" class="sidebar-link">项目介绍</a></li><li><a href="/docs/install.html" title="项目安装" class="sidebar-link">项目安装</a></li><li><a href="/docs/project-introduce.html" title="目录结构" class="sidebar-link">目录结构</a></li><li><a href="/docs/console.html" title="命令介绍" class="sidebar-link">命令介绍</a></li><li><a href="/docs/request.html" title="请求介绍" class="sidebar-link">请求介绍</a></li><li><a href="/docs/model.html" aria-current="page" title="模型介绍" class="active sidebar-link">模型介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/docs/model.html#模型介绍" title="模型介绍" class="sidebar-link">模型介绍</a></li><li class="sidebar-sub-header"><a href="/docs/model.html#catchmodel" title="CatchModel" class="sidebar-link">CatchModel</a></li><li class="sidebar-sub-header"><a href="/docs/model.html#catchqeury" title="CatchQeury" class="sidebar-link">CatchQeury</a></li></ul></li><li><a href="/docs/dataScope.html" title="权限介绍" class="sidebar-link">权限介绍</a></li><li><a href="/docs/extend.html" title="项目扩展" class="sidebar-link">项目扩展</a></li><li><a href="/docs/http.html" title="Http客户端" class="sidebar-link">Http客户端</a></li><li><a href="/docs/excel.html" title="Excel" class="sidebar-link">Excel</a></li><li><a href="/docs/sensitiveWord.html" title="敏感词" class="sidebar-link">敏感词</a></li><li><a href="/docs/crontab.html" title="定时任务新版" class="sidebar-link">定时任务新版</a></li><li><a href="/docs/catch-table.html" title="表格组件" class="sidebar-link">表格组件</a></li><li><a href="/docs/front.html" title="前端开发" class="sidebar-link">前端开发</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h2 id="模型介绍"><a href="#模型介绍" class="header-anchor">#</a> 模型介绍</h2> <p>在日常开发中，和我们打交道最多的就是数据库了。所以经常会使用到模型。<code>CatchAdmin</code>也提供非常便利的 <code>CURD</code> 操作。如果你想使用这些操作，必须使用<code>CatchModel</code>类。继承它，会让你的 <code>CURD</code>更加迅速稳定。</p> <p>看看他是如何定义的？在此之前的呢，先看一下模型如何定义的。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">class</span> Attachments <span class="token keyword">extends</span> <span class="token class-name">CatchModel</span>
<span class="token punctuation">{</span>
    <span class="token keyword">protected</span> <span class="token variable">$name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'attachments'</span><span class="token punctuation">;</span>
    
    <span class="token keyword">protected</span> <span class="token variable">$field</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
            <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token comment">// </span>
			<span class="token string single-quoted-string">'path'</span><span class="token punctuation">,</span> <span class="token comment">// 附件存储路径</span>
            <span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span> <span class="token comment">// 资源地址</span>
			<span class="token string single-quoted-string">'mime_type'</span><span class="token punctuation">,</span> <span class="token comment">// 资源mimeType</span>
			<span class="token string single-quoted-string">'file_ext'</span><span class="token punctuation">,</span> <span class="token comment">// 资源后缀</span>
			<span class="token string single-quoted-string">'file_size'</span><span class="token punctuation">,</span> <span class="token comment">// 资源大小</span>
			<span class="token string single-quoted-string">'filename'</span><span class="token punctuation">,</span> <span class="token comment">// 资源名称</span>
			<span class="token string single-quoted-string">'driver'</span><span class="token punctuation">,</span> <span class="token comment">// local,oss,qcloud,qiniu</span>
			<span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token comment">// 创建时间</span>
			<span class="token string single-quoted-string">'updated_at'</span><span class="token punctuation">,</span> <span class="token comment">// 更新时间</span>
			<span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">,</span> <span class="token comment">// 删除时间</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>像这样的形式的模型，都是通过命令生成的。无需你手动填写。所以 <code>CatchAdmin</code> 中所有的模型拥有<code>Field</code> 属性，它就是表字段的映射，这在之后的 <code>CURD</code> 中很有必要。</p> <h2 id="catchmodel"><a href="#catchmodel" class="header-anchor">#</a> CatchModel</h2> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">CatchModel</span> <span class="token keyword">extends</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>think<span class="token punctuation">\</span>Model</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 除了 softDelete 其他三个 trait 都是 catchadmin 中使用的。你可以用到任何你想用的地方</span>
    <span class="token keyword">use</span> <span class="token package">SoftDelete</span><span class="token punctuation">,</span> TransTrait<span class="token punctuation">,</span> BaseOptionsTrait<span class="token punctuation">,</span> ScopeTrait<span class="token punctuation">;</span>

    <span class="token comment">// 自定义创建时间字段</span>
    <span class="token keyword">protected</span> <span class="token variable">$createTime</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">;</span>
    <span class="token comment">// 自定义更新时间字段</span>
    <span class="token keyword">protected</span> <span class="token variable">$updateTime</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'updated_at'</span><span class="token punctuation">;</span>
    <span class="token comment">// 自定义删除字段</span>
    <span class="token keyword">protected</span> <span class="token variable">$deleteTime</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">;</span>
    <span class="token comment">// 默认删除的值</span>
    <span class="token keyword">protected</span> <span class="token variable">$defaultSoftDelete</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment">// 自动写入时间戳</span>
    <span class="token keyword">protected</span> <span class="token variable">$autoWriteTimestamp</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span>

    <span class="token keyword">public</span> <span class="token keyword">const</span> <span class="token constant">LIMIT</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>

    <span class="token comment">// 开启</span>
    <span class="token keyword">public</span> <span class="token keyword">const</span> <span class="token constant">ENABLE</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment">// 禁用</span>
    <span class="token keyword">public</span> <span class="token keyword">const</span> <span class="token constant">DISABLE</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>一目了然。他使用软删除。所以如果你不想使用软删除，可以直接继承 <code>\think\Model</code>。他还使用了三个<code>trait</code>,这三个<code>trait</code>就是 <code>CURD</code> 操作的保证。</p> <h3 id="baseoptionstrait"><a href="#baseoptionstrait" class="header-anchor">#</a> BaseOptionsTrait</h3> <p>看一下它所提供的有哪些方法，在下面会一一说明。</p> <div class="language-php extra-class"><pre class="language-php"><code>    <span class="token comment">// 列表查询方法</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 新增单条数据</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">storeBy</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 循环插入数据时使用</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">createBy</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 更新数据</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">updateBy</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">,</span> <span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$field</span> <span class="token operator">=</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 查找数据</span>
    <span class="token comment">// 第一个是 id</span>
    <span class="token comment">// 第二个是查询的字段</span>
    <span class="token comment">// 第三个 true 可查询软删除的数据</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">findBy</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$field</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$trash</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 删除数据</span>
    <span class="token comment">// force true 可物理删除数据</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">deleteBy</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">,</span> <span class="token variable">$force</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 批量插入数据</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">insertAllBy</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 软删除恢复</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">recover</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
    
    <span class="token comment">// 获取删除字段</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getDeleteAtField</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 别名字段 自动添加当前模型的表名</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">aliasField</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 禁用/启用 如果表里面有 status 字段默认使用，当然也可以自定义字段</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">disOrEnable</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">,</span> <span class="token variable">$field</span><span class="token operator">=</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><h3 id="transtrait"><a href="#transtrait" class="header-anchor">#</a> TransTrait</h3> <p>这是关于事务操作的方法，轻松使用模型操作，丢弃 <code>Db::startTrans</code>, 这种不是很便利的操作。使用方法和文档是一样的，无需担心。</p> <div class="language-php extra-class"><pre class="language-php"><code>    <span class="token comment">// 开启事务</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">startTrans</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 提交事务</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 回滚事务</span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

    <span class="token comment">// 事务组的操作  </span>
    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">transaction</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified type-declaration"><span class="token punctuation">\</span>Closure</span> <span class="token variable">$function</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span>
</code></pre></div><p>在继承<code>CatchModel</code>之后你可以直接在模型里面使用，eg.而且有非常友好的提示。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">startTrans</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="scopetrait"><a href="#scopetrait" class="header-anchor">#</a> ScopeTrait</h3> <p>范围查询只提供了创建者查询。非常方便。</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">scopeCreator</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>使用有很好的案例,例如</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">catchSearch</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">catchOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">creator</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><div class="custom-block warning"><p class="custom-block-title">WARNING</p> <p>当你使用范围查询的 <code>creator</code> 方法时候，必须放在 <code>field</code> 方法之后，而且不能单独使用，必须强制使用 <code>field</code> 方法进行查询。</p></div> <h3 id="搜索"><a href="#搜索" class="header-anchor">#</a> 搜索</h3> <p><code>catchadmin</code> 默认使用框架的搜索器，所以你只需要使用搜索器就好了，搜索的参数被<code>CatchAdmin</code>过滤了。具体搜索器的用法可以看<a href="https://www.kancloud.cn/manual/thinkphp6_0/1037590" target="_blank" rel="noopener noreferrer">框架文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="catchqeury"><a href="#catchqeury" class="header-anchor">#</a> CatchQeury</h2> <p><code>CatchQuery</code>在框架起到了很重要的作用，它继承了框架 <code>Query</code>, 并且新写了很多方法，这些方法在开发中起到很大的作用。所以将这些方法运用到你的开发之中，会事半功倍。</p> <h3 id="重写的方法"><a href="#重写的方法" class="header-anchor">#</a> 重写的方法</h3> <div class="language-php extra-class"><pre class="language-php"><code>  
   <span class="token comment">// model Join 的模型 strigin/array </span>
  <span class="token comment">// oinField join 模型的字段</span>
  <span class="token comment">// currentJoinField 当前模型的关联的字段</span>
  <span class="token comment">// field 需要 join 的模型查询的字段</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">catchJoin</span><span class="token punctuation">(</span><span class="token keyword type-hint">mixed</span> <span class="token variable">$model</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$joinField</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$currentJoinField</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$field</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$type</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'INNER'</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$bind</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">CatchQuery</span>
  <span class="token comment">// 有时候会有这个需求，连接同一张表两次，那么连接的表就需要两个不同的别名，那么就可以这么做</span>
  <span class="token comment">// -&gt;catchJoin([Users::class, 'u'], 'id', 'created_at', ['username as uname'])</span>
  <span class="token comment">// -&gt;catchJoin([Users::class, 'us'], 'id', 'created_at', ['username as us_name'])</span>


  <span class="token comment">// 这个方法很常用</span>
  <span class="token comment">// 例如你表里有 10 个字段，但是偏偏有一字段你不需要，那么这个方法可以帮你过滤掉该字段 </span>
  <span class="token comment">// ！注意 是主表的字段</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">withoutField</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$needAlias</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span>

  <span class="token comment">// 调用它 配合搜索器完美实现搜索  </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">catchSearch</span><span class="token punctuation">(</span><span class="token variable">$params</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">CatchQuery</span>

  <span class="token comment">// 获取 alias 字段  </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getAlias</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
   
  <span class="token comment">// like 查询 默认 both %%</span>
  <span class="token comment">// option =&gt; left %xxx</span>
  <span class="token comment">// option =&gt; right xxx% </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">whereLike</span><span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$condition</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$logic</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'AND'</span><span class="token punctuation">,</span> <span class="token variable">$option</span> <span class="token operator">=</span><span class="token string single-quoted-string">'both'</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Query</span>

  <span class="token comment">// 增加额外字段 在有必要的时候可以添加  </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">addFields</span><span class="token punctuation">(</span><span class="token variable">$fields</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">CatchQuery</span>

  <span class="token comment">// 分页</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">paginate</span><span class="token punctuation">(</span><span class="token variable">$listRows</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$simple</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Paginator</span>

  <span class="token comment">// 排序</span>
  <span class="token comment">// 如果你使用了它，并且存在 sort 字段 那么最后的结果就是这样的</span>
  <span class="token comment">// order('sort')-&gt;order('id') 很方便有么有</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">catchOrder</span><span class="token punctuation">(</span><span class="token variable">$order</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span>

  <span class="token comment">// 添加子查询  </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span>  <span class="token function">addSelectSub</span><span class="token punctuation">(</span><span class="token keyword type-hint">callable</span> <span class="token variable">$callable</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$as</span><span class="token punctuation">)</span>

  <span class="token comment">// 字段自增</span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">increment</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$amount</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token comment">// 字段自减  </span>
  <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">decrement</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$amount</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span>


</code></pre></div><h3 id="自动写入关联关系"><a href="#自动写入关联关系" class="header-anchor">#</a> 自动写入关联关系</h3> <p>模型加入 with 属性</p> <p>例如文章和评论一对多的关系</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">comments</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name class-name-fully-qualified return-type"><span class="token punctuation">\</span>think<span class="token punctuation">\</span>model<span class="token punctuation">\</span>relation<span class="token punctuation">\</span>HasMany</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">hasMany</span><span class="token punctuation">(</span><span class="token class-name static-context">Comments</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'article_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>那么只需要在 Article 模型中定义 with 属性</p> <div class="language- extra-class"><pre class="language-text"><code>class Article
{
    protected $with = ['comments']
}
</code></pre></div><p>定义好之后，每次查询都会自动关联了。</p> <p>另外还提供两个额外的放</p> <h3 id="去除不需要的关联关系"><a href="#去除不需要的关联关系" class="header-anchor">#</a> 去除不需要的关联关系</h3> <p>例如 <code>protected $with = ['comments', 'tags']</code> 时候，如果不需要 <code>comments</code> 关联关系</p> <div class="language-php extra-class"><pre class="language-php"><code>article<span class="token operator">-&gt;</span><span class="token function">withoutRelation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="仅仅需要某个关联关系"><a href="#仅仅需要某个关联关系" class="header-anchor">#</a> 仅仅需要某个关联关系</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token string backtick-quoted-string">``</span>`php
article<span class="token operator">-&gt;</span><span class="token function">withOnlyRelation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><div class="language- extra-class"><pre class="language-text"><code>
</code></pre></div></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/catch-admin/document/edit/master/docs/docs/model.md" target="_blank" rel="noopener noreferrer">在 Github 编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">更新时间:</span> <span class="time">11/23/2020, 6:16:27 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/docs/request.html" class="prev"><i aria-label="icon: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        请求介绍
      </a></span> <span class="next"><a href="/docs/dataScope.html">
        权限介绍
        <i aria-label="icon: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.d0396f44.js" defer></script><script src="/assets/js/2.1cef47df.js" defer></script><script src="/assets/js/17.aac0bf62.js" defer></script>
  </body>
</html>